package com.lanou.zakerproject.city;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.BaseActivity;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.tool.GsonRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class CityActivity extends BaseActivity {
    private ExpandableListView expandableListView;
    private CityAdapter cityAdapter;

    List<String> letter;
    HashMap<String, List<AllCityData.Citys>> city;
    HashSet<String> has;

    private String url = "http://183.232.160.141/app.api.autohome.com.cn/autov5.2.0/news/province-pm2-ts0.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView_city);
        city = new HashMap<String, List<AllCityData.Citys>>();
        letter = new ArrayList<String>();
        has = new HashSet<String>();


        cityAdapter = new CityAdapter(CityActivity.this, letter, city);
        expandableListView.setAdapter(cityAdapter);

        getRequsetData(url);
        //设置 属性 GroupIndicator 去掉默认向下的箭头
        expandableListView.setGroupIndicator(null);

    }

    List<String> temp = new ArrayList<String>();

    void rmList() {
        temp.clear();
        for (int kk = 0; kk < letter.size(); kk++) {
            if (!has.contains(letter.get(kk))) {
                temp.add(letter.get(kk));
            }
        }
        for (int i = 0; i < temp.size(); i++) {
            letter.remove(temp.get(i));
        }
    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<AllCityData> gsonRequest = new GsonRequest<>(urls, AllCityData.class, new Response.Listener<AllCityData>() {
            @Override
            public void onResponse(AllCityData response) {
                String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                for (int i = 0; i < data.length(); i++) {
                    letter.add(data.charAt(i) + "");
                }
                for (int i = 0; i < letter.size(); i++) {
                    city.put(letter.get(i), new ArrayList<AllCityData.Citys>());
                }
                for (int i = 0; i < response.getResult().getProvinces().size(); i++) {
                    for (int j = 0; j < response.getResult().getProvinces().get(i).getCitys().size(); j++) {
                        String key = response.getResult().getProvinces().get(i).getCitys().get(j).getFirstletter();
                        AllCityData.Citys value = response.getResult().getProvinces().get(i).getCitys().get(j);
                        city.get(key).add(value);
                        has.add(key);
                    }
                }
                rmList();
                cityAdapter.notifyDataSetChanged();
                //将所有项设置成默认展开
                int groupCount = expandableListView.getCount();
                for (int i = 0; i < groupCount; i++) {
                    expandableListView.expandGroup(i);
                }
                expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                        return true;
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }


}
